package zust.se.delicious.service.impl;

import java.sql.Timestamp;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import zust.se.delicious.dao.BookmarkDao;
import zust.se.delicious.dao.TagsDao;
import zust.se.delicious.dao.UserDao;
import zust.se.delicious.domain.Bookmark;
import zust.se.delicious.domain.Tags;
import zust.se.delicious.domain.User;
import zust.se.delicious.service.BookmarkService;
import zust.se.delicious.vo.BookmarkBean;
import zust.se.delicious.vo.PageBean;

public class BookmarkServiceImpl implements BookmarkService {

	private UserDao userDao;

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	private TagsDao tagsDao;
	private BookmarkDao bookmarkDao;

	public BookmarkBean getBmById(int bid) {
		return convertToBean(bookmarkDao.get(bid));
	}

	@Override
	public boolean saveBookmark(BookmarkBean bookmarkbean) {
		Bookmark bm = convertToDomin(bookmarkbean);
		bm.setCreateTime(new Timestamp(System.currentTimeMillis()));
		int result = bookmarkDao.save(bm);
		if (result > 0)
			return true;
		else
			return false;
	}

	@Override
	public void updateBookmark(BookmarkBean bookmarkbean) {
		Bookmark bm = convertToDomin(bookmarkbean);
		bm.setCreateTime(bookmarkDao.get(bm.getId()).getCreateTime());
		bookmarkDao.update(bm);
	}

	private Set<Tags> saveTags(String tags) {
		HashSet<Tags> tagSet = new HashSet<Tags>();
		String[] tagNameArray = tags.split(",");
		for (String tagName : tagNameArray) {
			Tags tag = tagsDao.get(tagName);
			if (tag == null) {
				tag = new Tags();
				tag.setTags_name(tagName);
				tagsDao.save(tag);
			}
			tagSet.add(tag);
		}
		return tagSet;
	}

	public void setTagsDao(TagsDao tagsDao) {
		this.tagsDao = tagsDao;
	}

	public void setBookmarkDao(BookmarkDao bookmarkDao) {
		this.bookmarkDao = bookmarkDao;
	}

	@Override
	public List<BookmarkBean> getList(int userId, PageBean pageBean) {
		pageBean.setTotal(bookmarkDao.countBmList(userId).intValue());
		List<Bookmark> blist = bookmarkDao.findBmList(new User(userId), pageBean.getCurrent(), pageBean.getSize());
		List<BookmarkBean> bblist = convertToBeanList(blist);
		return bblist;
	}

	public BookmarkBean convertToBean(Bookmark bookmark) {
		BookmarkBean bean = new BookmarkBean();
		bean.setCreatTime(bookmark.getCreateTime().getTime());
		bean.setDescription(bookmark.getDescription());
		bean.setId(bookmark.getId());
		bean.setIspublic(bookmark.isPublicFlag() ? "on" : "");
		bean.setUrl(bookmark.getUrl());
		bean.setUser_id(bookmark.getUser().getId());
		bean.setUsername(bookmark.getUser().getUsername());
		bean.setAvatar(bookmark.getUser().getAvatar());
		bean.setTags(convertTag(bookmark.getTagsSet()));
		bean.setTitle(bookmark.getTitle());
		bean.setSaveNum(bookmarkDao.countBmByUrl(bookmark.getUrl()).intValue());
		return bean;
	}

	public Bookmark convertToDomin(BookmarkBean bookmarkbean) {
		Bookmark bookmark = new Bookmark();
		bookmark.setId(bookmarkbean.getId());
		bookmark.setDescription(bookmarkbean.getDescription());
		bookmark.setTitle(bookmarkbean.getTitle());
		bookmark.setUrl(bookmarkbean.getUrl());
		bookmark.setUser(userDao.get(bookmarkbean.getUser_id()));
		bookmark.setTagsSet(saveTags(bookmarkbean.getTags()));
		bookmark.setPublicFlag((bookmarkbean.getIspublic() != null && bookmarkbean.getIspublic().equals("on")) ? true : false);
		return bookmark;
	}

	private String convertTag(Set<Tags> tagSet) {
		String tags = "";
		for (Tags tag : tagSet) {
			tags += (tag.getTags_name() + ",");
		}
		System.out.println("tags=" + tags);
		return tags;
	}

	@Override
	public List<BookmarkBean> getFeed(int userId, PageBean pageBean) {
		pageBean.setTotal(bookmarkDao.countBmFeed(userId).intValue());
		List<Bookmark> blist = bookmarkDao.findBmFeed(new User(userId), pageBean.getCurrent(), pageBean.getSize());
		List<BookmarkBean> bblist = convertToBeanList(blist);
		return bblist;
	}

	@Override
	public List<BookmarkBean> searchMyList(String keyword, int uid) {
		List<Bookmark> blist = bookmarkDao.searchMyList(keyword, uid);
		List<BookmarkBean> bblist = convertToBeanList(blist);
		return bblist;
	}

	@Override
	public List<BookmarkBean> searchOtherList(String keyword, int uid) {
		List<Bookmark> blist = bookmarkDao.searchOtherList(keyword, uid);
		List<BookmarkBean> bblist = convertToBeanList(blist);
		return bblist;
	}

	@Override
	public List<BookmarkBean> searchAllList(String keyword) {
		List<Bookmark> blist = bookmarkDao.searchAllList(keyword);
		List<BookmarkBean> bblist = convertToBeanList(blist);
		return bblist;
	}

	public List<BookmarkBean> convertToBeanList(List<Bookmark> blist) {
		List<BookmarkBean> bblist = new LinkedList<BookmarkBean>();
		for (Bookmark bookmark : blist) {
			if (bookmark != null)
				bblist.add(convertToBean(bookmark));
		}
		return bblist;
	}

	@Override
	public List<Tags> searchTagList(String keyword) {
		return tagsDao.findTagsByKeyword(keyword);
	}

	@Override
	public List<BookmarkBean> matchMyList(String url, int uid) {
		List<Bookmark> blist = bookmarkDao.matchMyList(url, uid);
		List<BookmarkBean> bblist = convertToBeanList(blist);
		return bblist;
	}

	@Override
	public List<BookmarkBean> matchOtherList(String url, int uid) {
		List<Bookmark> blist = bookmarkDao.matchchOtherList(url, uid);
		List<BookmarkBean> bblist = convertToBeanList(blist);
		return bblist;
	}

	@Override
	public List<BookmarkBean> matchAllList(String url) {
		List<Bookmark> blist = bookmarkDao.mathchAllList(url);
		List<BookmarkBean> bblist = convertToBeanList(blist);
		return bblist;
	}

	@Override
	public List<Tags> matchTagList(String url) {
		return tagsDao.findTagsByUrl(url);
	}

}
